package control;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.tools.ant.types.CommandlineJava.SysProperties;

public class Ex10 {
/**
 * @A vampire number has an even number of digits and is formed by 
 * multiplying a pair of numbers containing half the number of digits
 * of the result. The digits are taken from the original number in any order. 
 * Pairs of trailing zeroes are not allowed. Examples include:
	1260 = 21 * 60
	1827 = 21 * 87
	2187 = 27 * 81
	Write a program that finds all the 4-digit vampire numbers.
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		// initializam un array care sa tina toate numerele vampir.
		
		ArrayList<String> listaNumereVampir = new ArrayList<String>();
		
		//117067
		//1260
		//13078260
		//6880 are bug
		
		int numarVampir = 6880 ; // numarul vampir
		
		for (int x = 1000; x<10000 ; x++){
			numarVampir = x;
		
				
		// aduce un array cu cifrele numarului
		ArrayList<Integer> listaCifre = new Ex10().getListOfDigitsInt(numarVampir); 
		
		// numerele vampir trebuie sa aiba un numar par de cifre.
		if(listaCifre.size()%2!=0)
		{
			System.out.println("NUMARUL NU ARE un numar de cifre PAR");
			return;
		}
		
		// instantiem Ext10
		
		
		// aducem lista tuturor permutarilor posibile 
		//    cu pasul = jumatate din numarul de cifre al numarului vampir
		//Ex10 inst = new Ex10();
		//HashSet<List<Integer>> listaFangs = inst.intoarcePermutariCuPas(listaCifre,listaCifre.size()/2);
		PermutariMau<List<Integer>> instOfPemutariClass = new PermutariMau<List<Integer>>();
		HashSet<List<Integer>> listaFangs = instOfPemutariClass.intoarcePermutariCuPas(listaCifre,listaCifre.size()/2);
		
		
		// declaram un iterator pentru a accesa perechile
		Iterator<List<Integer>> iteratorFangs = listaFangs.iterator();
		
		ArrayList<Integer> listaPerechi = new ArrayList<Integer>(); // va tine perechile de fangs
		
		// concatenam numerele din fiecare pereche si le adaugam intr-un array
		while (iteratorFangs.hasNext()){
			List<Integer> fang = iteratorFangs.next();
			if (fang.get(0)!=0)
			{
				listaPerechi.add( 
					Integer.parseInt(	
							""+fang.get(0)+fang.get(1)
				  )		
				);
			}
		}
		int primElement = 0;
		
//		System.out.println("======="+listaPerechi+"=======");
		for (int i=0;i<listaPerechi.size();i++){
//			System.out.println("SIZE ESTE = " + listaPerechi.size());
			 primElement = listaPerechi.get(0);
//			 System.out.println("Scoatem ELEMENTUL 0: " + listaPerechi.get(0)); 
			listaPerechi.remove(0);
			
		
			Iterator<Integer> tempIterator = listaPerechi.iterator();
//			System.out.println("Lista ramasa este: ------"+listaPerechi+"------");
			while (tempIterator.hasNext()){
				int tempNumber = tempIterator.next();
//				System.out.println(" La-nceput de WHILE : HAS NEXT?!? :" +tempIterator.hasNext() + " si ESTE: "+tempNumber );
				//System.out.println("Prim element: " + primElement + " X " + "Temp Number: " + tempNumber);
//				System.out.println(i + " este i");
//				System.out.println(tempNumber + " este nr temp");
//				System.out.println(primElement+" X " +tempNumber+" = " +primElement*tempNumber);
				if (primElement*tempNumber == numarVampir){
					listaNumereVampir.add(numarVampir+" = " + primElement + " x " + tempNumber);
					System.out.println( "AVEEEEM UUUN NUUUMAR VAMPIIIIR: " + numarVampir);
//					return;
					//break;
				}
//				System.out.println("@@------"+listaPerechi+"------@@");
//				System.out.println("URMATOR: " + tempNumber);
//				System.out.println("has next : " + tempIterator.hasNext());
				if (!tempIterator.hasNext() && 
					listaPerechi.size()==2 && 
					listaPerechi.get(0)*listaPerechi.get(1) == numarVampir)
				{
					listaNumereVampir.add(numarVampir+" = " + primElement + " x " + tempNumber);
					System.out.println( "AVEEEEM UUUN NUUUMAR VAMPIIIIR: " + numarVampir);
//					return;
					//break;
					
				}
			}
			
			}
		System.out.println(listaNumereVampir);
		}
	}
		
	

	@SuppressWarnings("unused")
	private Collection<List<Integer>> intoarcePermutari(ArrayList<String> listaCifre)
	{
		PermutariMau<Integer> obj = new PermutariMau<Integer>();
        Collection<Integer> input = new ArrayList<Integer>();
        if (listaCifre.size()==0) return null;
      	for (String i : listaCifre)
      	{
      		input.add(Integer.parseInt(i));
        }
        Collection<List<Integer>> output = obj.permute(input);
        int k = 0;
        Set<List<Integer>> pnr = null;
        for (int i = 0; i <= input.size(); i++) {
            pnr = new HashSet<List<Integer>>();
            for(List<Integer> integers : output){
            pnr.add(integers.subList(i, integers.size()));
            }
            k = input.size()- i;
//            System.out.println("P("+input.size()+","+k+") :"+ 
//            "Count ("+pnr.size()+") :- "+pnr);
        }

        return output;
	}
	
	private HashSet<List<Integer>> intoarcePermutariCuPas(ArrayList<Integer> listaCifre, int pas)
	{
		PermutariMau<Integer> obj = new PermutariMau<Integer>();
        Collection<Integer> input = new ArrayList<Integer>();
        if (listaCifre.size()==0) return null;
        for (int i : listaCifre){
        input.add(i);
        }
        
        Collection<List<Integer>> output = obj.permute(input);
        
        HashSet<List<Integer>> pnr = null;
        pnr = new HashSet<List<Integer>>();
        for(List<Integer> integers : output){
        	pnr.add(integers.subList(pas, integers.size()));
        }
        //int k = 0;
        //k = input.size()- pas;
//        System.out.println("P("+input.size()+","+k+") :" + "Count ("+pnr.size()+") :- "+pnr);
        return  pnr;
	}
		
		
		
	@SuppressWarnings("unused")
	private ArrayList<Integer> getListOfDigitsInt(int numar)
	{
		String numarString = Integer.toString(numar);
		ArrayList<Integer> lista = new ArrayList<Integer>();
		for (int i=1;i<=numarString.length();i++){
			lista.add(Integer.parseInt(numarString.substring(i-1, i)));
		}
		return lista;
	}
	@SuppressWarnings("unused")
	private ArrayList<String> getListOfDigitsStr(int numar)
	{
		String numarString = Integer.toString(numar);
		ArrayList<String> lista = new ArrayList<String>();
		for (int i=1;i<=numarString.length();i++){
			lista.add(numarString.substring(i-1, i));
		}
		return lista;
	}

}
